﻿Imports tSvcControl
Imports System.Configuration

Public Class tSvcWrapper

    Private terrariaProcesses As New tServerCollection
    Private terrariaConfig As tSvcControl.tConfiguration

    Protected Overrides Sub OnStart(ByVal args() As String)
        ' Add code here to start your service. This method should set things
        ' in motion so your service can do its work.
        Try
            Dim terrariaConfig As New tConfiguration

            'read configuration from App.config
            terrariaConfig.ReadFromConfig()

            'Attach event handler to process events
            AddHandler terrariaProcesses.LogEvent, AddressOf LogProcessEvents

            'Start all the servers with appropriate configuration
            terrariaProcesses.StartAllServers(terrariaConfig)

            'wait a quarter of a second before issuing world selection to TShock servers
            Threading.Thread.Sleep(250)
            terrariaProcesses.selectTShockWorldAllServers()

            'wait a quarter of a second before issuing port selection to TShock servers
            Threading.Thread.Sleep(250)
            terrariaProcesses.selectTShockPortAllServers()

            'set autosave
            Me.SetAutosave()
        Catch ex As Exception
            'TODO: Must get rid of these hardcodes!
            'write unhandled exception to log
            EventLog.WriteEntry(String.Concat("Failed to start service!", Environment.NewLine, ex.ToString), EventLogEntryType.Error)

            'tell the world somethign nasty just happenned
            Me.ExitCode = -1

            'stop the service
            Me.Stop()
        End Try

    End Sub

    Protected Overrides Sub OnStop()
        'Stop auto-save feature
        tmrAutoSave.Enabled = False

        'TODO: Must get rid of these hardcodes!
        ' Warn users of stoppage.
        terrariaProcesses.BroadcastAllServers("shutting down server in... 5")
        Threading.Thread.Sleep(1000)
        terrariaProcesses.BroadcastAllServers("shutting down server in... 4")
        Threading.Thread.Sleep(1000)
        terrariaProcesses.BroadcastAllServers("shutting down server in... 3")
        Threading.Thread.Sleep(1000)
        terrariaProcesses.BroadcastAllServers("shutting down server in... 2")
        Threading.Thread.Sleep(1000)
        terrariaProcesses.BroadcastAllServers("shutting down server in... 1")
        Threading.Thread.Sleep(1000)

        'Shutdown servers
        terrariaProcesses.StopAllServers()

    End Sub

    Protected Overrides Sub OnShutdown()
        MyBase.OnShutdown()

        'Stop auto-save
        tmrAutoSave.Enabled = False

        'TODO: Must get rid of these hardcodes!
        'Very brief warning to users
        terrariaProcesses.BroadcastAllServers("shutting down server due to reboot...")
        Threading.Thread.Sleep(750)

        'Shutdown server
        terrariaProcesses.StopAllServers()

    End Sub

    Private Sub tmrAutoSave_Tick(sender As Object, e As EventArgs) Handles tmrAutoSave.Tick
        'call the server's save command
        terrariaProcesses.AutoSaveAllServers()
    End Sub


    Private Sub SetAutosave()
        Try
            'set interval and start timer if Autosave is enabled
            Me.tmrAutoSave.Interval = terrariaConfig.AutoSave.IntervalInMilliseconds
            Me.tmrAutoSave.Enabled = terrariaConfig.AutoSave.Enabled
        Catch ex As Exception
            'TODO: Must get rid of these hardcodes!
            'somethign was wrong with autosave. Dont re-throw as this is not fatal.
            EventLog.WriteEntry(String.Concat("Failed to initialize auto-save!", Environment.NewLine, ex.ToString), EventLogEntryType.Warning)
        End Try
    End Sub


    Private Sub LogProcessEvents(ByVal serverName As String, ByVal eventMessage As String, ByVal innerException As Exception, ByVal eventType As EventLogEntryType)
        'TODO: Must get rid of these hardcodes!
        'Log inner servers events to EventLog
        EventLog.WriteEntry(String.Concat("Server: ", serverName, " ", eventMessage, Environment.NewLine, innerException.ToString), eventType)
    End Sub

End Class
